//
//  SDWeiXinPhotoContainerView.m
//  SDAutoLayout 测试 Demo
//
//  Created by gsd on 15/12/23.
//  Copyright © 2015年 gsd. All rights reserved.
//

#import "SDWeiXinPhotoContainerView.h"

#import "UIView+SDAutoLayout.h"

#import "SDPhotoBrowser.h"

@interface SDWeiXinPhotoContainerView () <SDPhotoBrowserDelegate>
{
    NSInteger _index;
}
@end

@implementation SDWeiXinPhotoContainerView


- (void)setPicPathStringsArray:(NSMutableArray *)picPathStringsArray
{
    _picPathStringsArray = picPathStringsArray;
    
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    if (_picPathStringsArray.count == 0) {
        self.height = 0;
        self.fixedHeight = @(0);
        return;
    }
    long perRowItemCount = [self perRowItemCountForPicPathArray:_picPathStringsArray];
    CGFloat itemW = [self itemWidthForPicPathArray:_picPathStringsArray] ;
    if (_isAdd) {
        itemW = (kScreenWidth - 60)/5;
        perRowItemCount = 5;
    }
    CGFloat itemH = 0;
    if (_picPathStringsArray.count == 1) {
        UIImage *image = [UIImage imageWithData:_picPathStringsArray.firstObject];
        if (image.size.width) {
            itemH = image.size.height / image.size.width * itemW;
        }
        if (_isAdd) {
            itemH = itemW;
        }
    } else {
        itemH = itemW;
    }
    
    CGFloat margin = 10;
    
    [_picPathStringsArray enumerateObjectsUsingBlock:^(NSData *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        long columnIndex = idx % perRowItemCount;
        long rowIndex = idx / perRowItemCount;
        UIImageView *imageView = [UIImageView new];
        imageView.image = [UIImage imageWithData:obj];
        imageView.frame = CGRectMake(10 + columnIndex * (itemW + margin), 10 + rowIndex * (itemH + margin), itemW, itemH);
        [self addSubview:imageView];
        
        imageView.userInteractionEnabled = YES;
        imageView.tag = idx;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)];
        [imageView addGestureRecognizer:tap];
    }];
    
    CGFloat w = perRowItemCount * itemW + (perRowItemCount + 1) * margin;
    int columnCount = ceilf(_picPathStringsArray.count * 1.0 / perRowItemCount);
    CGFloat h = columnCount * itemH + (columnCount + 1) * margin;
    self.width = w;
    self.height = h;
    
    self.fixedHeight = @(h);
    self.fixedWith = @(w);
}

- (void)tapImageView:(UITapGestureRecognizer *)tap
{
    UIView *imageView = tap.view;
    SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];
    
    browser.currentImageIndex = imageView.tag;
    browser.sourceImagesContainerView = self;
    browser.imageCount = self.picPathStringsArray.count; 
    browser.delegate = self;
    self.browser = browser;
//    [browser.deleteBtn addTarget:self action:@selector(dec) forControlEvents:UIControlEventTouchUpInside];
    [browser show];
}
-(void)dec
{

}
- (CGFloat)itemWidthForPicPathArray:(NSArray *)array
{
    if (array.count == 1) {
        return 120;
    } else {
        return (kScreenWidth - 40)/3;
    }
}

- (NSInteger)perRowItemCountForPicPathArray:(NSArray *)array
{
    if (array.count < 3) {
        return array.count;
    } else if (array.count <= 4) {
        return 3     ;
    } else {
        return 3;
    }
}


#pragma mark - SDPhotoBrowserDelegate

- (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index
{
    _index = index;
    NSData *imageName = self.picPathStringsArray[index];
    NSURL *url = [NSURL URLWithString:[[NSString alloc]initWithData:imageName encoding:NSUTF8StringEncoding ]];
//    NSURL *url = [[NSBundle mainBundle] URLForResource:imageName withExtension:nil];
    return url;
}

- (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index
{
    UIImageView *imageView = self.subviews[index];
    return imageView.image;
}

@end
